<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>科泰聊天室</title>
  <link href="css/chat.css" rel="stylesheet">
  </link>
</head>

<body>
  <!-- TODO HTML代码直接复制完成的实战2代码 -->
  <div id="app">
    <h1 class="title center">科泰聊天室</h1>
    <text-father></text-father>
  </div>

  <template id="textFather">
    <div class="chat-window center">
      <div class="chat-messages">
        <div :class="{'message': true, [cht.type + '-message']: true}" v-for="(cht,index) in chat" :key="index">
          <div :class="{['message-'+cht.type]:true}"><img :src="cht.avatar" class="avatar" v-show="cht.type == 'received'"> <span
              class="message-content">{{cht.content}}</span><img :src="cht.avatar" class="avatar" v-show="cht.type == 'sent'"></div>
        </div>
      </div>
      <text-son @danji="change" :newText="newText"></text-son>
    </div>
  </template>

  <template id="textSon">
    <div class="chat-input"><input type="text" placeholder="请输入消息..." v-model="newText"> <button @click="soninput()">发送</button></div>
  </template>

  <script src="js/vue.js"></script>
  <script src="https://unpkg.com/axios@1.6.7/dist/axios.min.js"></script>
  <script>
    // TODO 将实战2写死的聊天记录，改为Axios请求data/chat-history.json 中的聊天记录数据
    var textSon = {
      template: '#textSon',
      props:['newText'],
      methods:{
        soninput(){
          this.$emit("danji",this.newText);
        }
      }
    }

    var textFather = {
      template: '#textFather',
      components: {
        'TextSon': textSon
      },
      data() {
        return {
          chat: [],
          newText:""
        }
      },
      methods:{
        loadNewsList(){
          axios.get('data/chat-history.json').then(resp=>{
          this.chat = resp.data
        });
        },
        change(value){
          this.newText = value;
          this.chat.push({type:'sent',avatar:'images/avt-02.png',content:this.newText})
        }
      },
      beforecreate(){

      },
      created(){
        this.loadNewsList();
      }
    }

    new Vue({
      el: '#app',
      components: {
        'TextFather': textFather
      }
    })
  </script>
</body>

</html>